package ex07.pyrmont.startup;


import ex07.pyrmont.core.SimpleContextLifecycleListenerV7;
import ex07.pyrmont.core.SimpleContextMapperV7;
import ex07.pyrmont.core.SimpleLoaderV7;
import ex07.pyrmont.core.SimpleContextV7;
import ex07.pyrmont.core.SimpleWrapperV7;
import org.apache.catalina.Connector;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Loader;
import org.apache.catalina.logger.FileLogger;
import org.apache.catalina.Mapper;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.http.HttpConnector;

public final class BootstrapV7 {
    public static void main(String[] args) {
        Connector connector = new HttpConnector();
        Wrapper wrapper1 = new SimpleWrapperV7();
        wrapper1.setName("Primitive");
        wrapper1.setServletClass("PrimitiveServlet");
        Wrapper wrapper2 = new SimpleWrapperV7();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");
        Loader loader = new SimpleLoaderV7();

        Context context = new SimpleContextV7();
        context.addChild(wrapper1);
        context.addChild(wrapper2);

        Mapper mapper = new SimpleContextMapperV7();
        mapper.setProtocol("http");
        LifecycleListener listener = new SimpleContextLifecycleListenerV7();
        ((Lifecycle) context).addLifecycleListener(listener);
        context.addMapper(mapper);
        context.setLoader(loader);
        // context.addServletMapping(pattern, name);
        context.addServletMapping("/Primitive", "Primitive");
        context.addServletMapping("/Modern", "Modern");

        // ------ add logger --------
        System.setProperty("catalina.base", System.getProperty("user.dir"));
        FileLogger logger = new FileLogger();
        logger.setPrefix("FileLog_");
        logger.setSuffix(".txt");
        logger.setTimestamp(true);
        logger.setDirectory("webroot");
        context.setLogger(logger);

        //---------------------------

        connector.setContainer(context);
        try {
            connector.initialize();
            ((Lifecycle) connector).start();
            ((Lifecycle) context).start();

            // make the application wait until we press a key.
            System.in.read();
            ((Lifecycle) context).stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
